/*
给出两个 非空 的链表用来表示两个非负的整数。其中，它们各自的位数是按照 逆序 的方式存储的，并且它们的每个节点只能存储 一位 数字。
如果，我们将这两个数相加起来，则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外，这两个数都不会以 0 开头。

示例：

输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出：7 -> 0 -> 8
原因：342 + 465 = 807


 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        
    }
};
*/

#include<iostream>

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head=new ListNode(-1);
        ListNode* cur=head;
        ListNode* p1=l1;
        ListNode* p2=l2;
        int add=0;
        int val_1,val_2;
        while(p1!=NULL||p2!=NULL)
        {
            val_1=(p1!=NULL)?p1->val:0;
            val_2=(p2!=NULL)?p2->val:0;
            std::cout<<(val_1+val_2+add)<<" "<<val_1<<" "<<val_2<<" "<<add<<std::endl;
            cur->val=(val_1+val_2+add)%10;
            add=(val_1+val_2+add)/10;
            /*
            if((p1->next)!=NULL||(p2->next)!=NULL)
            {
                cur->next=new ListNode(0);
                cur=cur->next;
            }
            */
            if(p1!=NULL)
            {
                p1=p1->next;
            }
            if(p2!=NULL)
            {
                p2=p2->next;
            }
            
            if(p1==NULL&&p2==NULL)
            {
                break;
            }
            else
            {
                cur->next=new ListNode(0);
                cur=cur->next;
            }
            
        } 
        if (add==1)
        {
            cur->next=new ListNode(1);
        }        
        return head;
    }
};

/*
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* head=new ListNode(-1);
        ListNode* cur=head;
        ListNode* p1=l1;
        ListNode* p2=l2;
        int add=0;
        int val_1,val_2;
        while(p1!=nullptr||p2!=nullptr)
        {
            val_1=(p1!=nullptr)?p1->val:0;
            val_2=(p2!=nullptr)?p2->val:0;

            std::cout<<(val_1+val_2+add)<<" "<<val_1<<" "<<val_2<<" "<<add<<std::endl;
            cur->val=(val_1+val_2+add)%10;
            add=(val_1+val_2+add)/10;
            cur->next=new ListNode(0);
            cur=cur->next;
            p1=p1->next;
            p2=p2->next;
        }
        if (add==1)
        {
            cur->next=new ListNode(1);
        }
        return head->next;
    }
};
*/

int main()
{
    ListNode a_1(7);
    ListNode a_2(4);
    ListNode a_3(3);
    a_1.next=&a_2;
    a_2.next=&a_3;

    ListNode b_1(5);
    ListNode b_2(6);
    ListNode b_3(4);
    b_1.next=&b_2;
    b_2.next=&b_3;
    Solution s;
    
    ListNode* r=s.addTwoNumbers(&a_1,&b_1);

    while(r!=NULL)
    {
        std::cout<<r->val<<std::endl;
        r=r->next;
    }
    
    return 0;
}


// 2020.8.11 town 
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* last = NULL;
        int add = (l1->val + l2->val) / 10;
        int cur = (l1->val + l2->val) % 10;
        last = new ListNode(cur);
        ListNode* newnode = last;
        ListNode* tmp = NULL;
        l1 = l1 -> next;
        l2 = l2 -> next;
        int l1_num = 0;
        int l2_num = 0;
        while (l1 != NULL || l2 != NULL) {
            l1_num = 0;
            if (l1 != NULL){
                l1_num = l1 -> val;
            }
            l2_num = 0;
            if (l2 != NULL){
                l2_num = l2 -> val;
            }
            cur = l1_num + l2_num + add;
            add = cur / 10;
            cur = cur % 10;
            tmp = new ListNode(cur);
            last -> next = tmp;
            last = last -> next;
            if (l1 != NULL){
                l1 = l1 -> next;
            }
            if (l2 != NULL){
                l2 = l2 -> next;
            }
        }
        if (add > 0){
            tmp = new ListNode(add);
            last -> next = tmp;
            last = last -> next;
        }
        return newnode;
    }
};